package com.kgc.kmall.ribbon;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;
import java.util.concurrent.ThreadLocalRandom;

@Configuration
public class MySelfRule extends AbstractLoadBalancerRule{


    private int total=0;//被调用的次数默认0
    private int serverIndex=0;//当前的服务
    /**
     * Randomly choose from all living servers
     */
  //  @edu.umd.cs.findbugs.annotations.SuppressWarnings(value = "RCN_REDUNDANT_NULLCHECK_OF_NULL_VALUE")
    public Server choose(ILoadBalancer lb, Object key) {
        if (lb == null) {
            return null;
        }
        Server server = null;

        while (server == null) {
            if (Thread.interrupted()) {
                return null;
            }
            List<Server> upList = lb.getReachableServers();//获取到活着的服务
            List<Server> allList = lb.getAllServers();//获取到所有的服务

            int serverCount = allList.size();
            if (serverCount == 0) {
                /*
                 * No servers. End regardless of pass, because subsequent passes
                 * only get more restrictive.
                 */
                return null;
            }

//            int index = chooseRandomInt(serverCount);//随机获得一个服务的下标
//            server = upList.get(index);//从活着的服务中心获取到一个
            //=======================================================================================
            if(total<5){
                server=upList.get(serverIndex);
                System.out.println("上面的"+total);
                total++;
            }else{
                total=0;
                System.out.println("下面的"+total);
                serverIndex++;
                if(serverIndex>=upList.size()){
                    serverIndex=0;
                }
                server=upList.get(serverIndex);
            }
            //========================================================================================


            if (server == null) {
                /*
                 * The only time this should happen is if the server list were
                 * somehow trimmed. This is a transient condition. Retry after
                 * yielding.
                 */
                Thread.yield();
                continue;
            }

            if (server.isAlive()) {
                return (server);
            }

            // Shouldn't actually happen.. but must be transient or a bug.
            server = null;
            Thread.yield();
        }

        return server;

    }

    protected int chooseRandomInt(int serverCount) {
        return ThreadLocalRandom.current().nextInt(serverCount);
    }

    @Override
    public Server choose(Object key) {
        return choose(getLoadBalancer(), key);
    }

    @Override
    public void initWithNiwsConfig(IClientConfig clientConfig) {
        // TODO Auto-generated method stub

    }
}
